package com.art.museumbackend.service.impl;

import com.art.museumbackend.entity.Role;
import com.art.museumbackend.mapper.RoleMapper;
import com.art.museumbackend.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<Integer> getPermissionsByRoleId(Integer roleId) {
        return roleMapper.getPermissionsByRoleId(roleId);
    }

    @Override
    @Transactional
    public void updateRolePermissions(Integer roleId, List<Integer> permissionIds) {
        // 先删除原有的权限关联
        roleMapper.deleteRolePermissions(roleId);
        
        // 添加新的权限关联
        if (permissionIds != null && !permissionIds.isEmpty()) {
            for (Integer permissionId : permissionIds) {
                roleMapper.insertRolePermission(roleId, permissionId);
            }
        }
    }
} 